cd "C:\Users\evanl\Desktop\Polarization\Final Analysis"
use "C:\Users\evanl\Desktop\Polarization\Final Analysis\Analysis 1900 Data.dta", clear

set seed 577215664
xtset id year

// Excluding those cases with insufficient number of observations per groups to provide valid inference in fixed effects models
 drop if 	id==7 | id==9 | id==15 | id==46 | id==55 | id==58 | id==69 | ///
			id==80 | id==87 | id==89 | id==91 | id==95 | id==111 | id==113 | ///
			id==114 | id==117 | id==124 | id==125 | id==130 | id==135
			
xtreg lib ppol i.year if regime>0, fe vce(cluster id)
outreg2 using 1900a1.tex, replace adjr2 dec(3)  ctitle(Analysis Year)
xtreg lib L1.ppol i.year if regime>0, fe vce(cluster id)
outreg2 using 1900a1.tex, append adjr2 dec(3)  ctitle(One Year Lag)
xtreg lib L2.ppol i.year if regime>0, fe vce(cluster id)
outreg2 using 1900a1.tex, append adjr2 dec(3)  ctitle(Two Year Lag)
xtreg lib L5.ppol i.year if regime>0, fe vce(cluster id)
outreg2 using 1900a1.tex, append adjr2 dec(3)  ctitle(Five Year Lag)

xtreg lib ppol i.year if regime>0, fe vce(cluster id)
margins, at(ppol=(0(0.1)1))
marginsplot, scheme(s1mono) ///
	title("") ///
	ytitle("Predicted Value of Liberal Democracy") xtitle("V-Dem Political Polarization Measure")
graph export "C:\Users\evanl\Desktop\Polarization\Final Analysis\Figure 1a.pdf", as(pdf) replace
marginsplot, scheme(s1mono) ///
	title("") ///
	ytitle("Predicted Value of Liberal Democracy") xtitle("V-Dem Political Polarization Measure")
graph export "C:\Users\evanl\Desktop\Polarization\Final Analysis\Final Tables and Figures\Appendices\Appendix B Figure 3.tif", as(tif) replace

xtreg lib ppol i.year if regime>0 & PolChangeInd1==0, fe vce(cluster id)
outreg2 using 1900a2a.tex, replace adjr2 dec(3)  ctitle(Analysis Year)
xtreg lib L1.ppol i.year if regime>0 & PolChangeInd1==0, fe vce(cluster id)
outreg2 using 1900a2a.tex, append adjr2 dec(3)  ctitle(One Year Lag)
xtreg lib L2.ppol i.year if regime>0 & PolChangeInd1==0, fe vce(cluster id)
outreg2 using 1900a2a.tex, append adjr2 dec(3)  ctitle(Two Year Lag)
xtreg lib L5.ppol i.year if regime>0 & PolChangeInd1==0, fe vce(cluster id)
outreg2 using 1900a2a.tex, append adjr2 dec(3)  ctitle(Five Year Lag)

xtreg lib ppol i.year if regime>0 & PolChangeInd1==1, fe vce(cluster id)
outreg2 using 1900a2b.tex, replace adjr2 dec(3)  ctitle(Analysis Year)
xtreg lib L1.ppol i.year if regime>0 & PolChangeInd1==1, fe vce(cluster id)
outreg2 using 1900a2b.tex, append adjr2 dec(3)  ctitle(One Year Lag)
xtreg lib L2.ppol i.year if regime>0 & PolChangeInd1==1, fe vce(cluster id)
outreg2 using 1900a2b.tex, append  adjr2 dec(3) ctitle(Two Year Lag)
xtreg lib L5.ppol i.year if regime>0 & PolChangeInd1==1, fe vce(cluster id)
outreg2 using 1900a2b.tex, append adjr2 dec(3)  ctitle(Five Year Lag)

xtreg lib c.ppol##i.PolChangeInd1 i.year if regime>0, fe vce(cluster id)
outreg2 using 1900a2c.tex, replace adjr2 dec(3)  ctitle(Analysis Year)
xtreg lib c.L1.ppol##i.L1.PolChangeInd1 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a2c.tex, append  adjr2 dec(3) ctitle(One Year Lag)
xtreg lib c.L2.ppol##i.L2.PolChangeInd1 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a2c.tex, append adjr2 dec(3)  ctitle(Two Year Lag)
xtreg lib c.L5.ppol##i.L5.PolChangeInd1 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a2c.tex, append adjr2 dec(3)  ctitle(Five Year Lag)

xtreg lib c.ppol##i.PolChangeInd1  i.year if regime>0, fe vce(cluster id)
margins PolChangeInd1, at(ppol=(0(0.1)1))
marginsplot, scheme(s1mono) ///
	title("") 	plot( , label("All Other Cases" "Periods of Rapid Polarization")) ///
	ytitle("Predicted Value of Liberal Democracy") xtitle("V-Dem Political Polarization Measure")
graph export "C:\Users\evanl\Desktop\Polarization\Final Analysis\Figure 2a.pdf", as(pdf) replace
marginsplot, scheme(s1mono) ///
	title("") 	plot( , label("All Other Cases" "Periods of Rapid Polarization")) ///
	ytitle("Predicted Value of Liberal Democracy") xtitle("V-Dem Political Polarization Measure")
graph export "C:\Users\evanl\Desktop\Polarization\Final Analysis\Final Tables and Figures\Appendices\Appendix B Figure 4.tif", as(tif) replace


// Lets use the monikers elevated, heightened, and severe
//Elevated (above 0.4564323) - mean 

xtreg lib ppol i.year if regime>0 & PolLevelInd1==0, fe vce(cluster id)
outreg2 using 1900a3a-E.tex, replace adjr2 dec(3)  ctitle(Analysis Year)
xtreg lib L1.ppol i.year if regime>0 & PolLevelInd1==0, fe vce(cluster id)
outreg2 using 1900a3a-E.tex, append adjr2 dec(3)  ctitle(One Year Lag)
xtreg lib L2.ppol i.year if regime>0 & PolLevelInd1==0, fe vce(cluster id)
outreg2 using 1900a3a-E.tex, append  adjr2 dec(3) ctitle(Two Year Lag)
xtreg lib L5.ppol i.year if regime>0 & PolLevelInd1==0, fe vce(cluster id)
outreg2 using 1900a3a-E.tex, append  adjr2 dec(3) ctitle(Five Year Lag)

xtreg lib ppol i.year  if regime>0 & PolLevelInd1==1, fe vce(cluster id)
outreg2 using 1900a3b-E.tex, replace adjr2 dec(3)  ctitle(Analysis Year)
xtreg lib L1.ppol i.year if regime>0 & PolLevelInd1==1, fe vce(cluster id)
outreg2 using 1900a3b-E.tex, append  adjr2 dec(3) ctitle(One Year Lag)
xtreg lib L2.ppol i.year if regime>0 & PolLevelInd1==1, fe vce(cluster id)
outreg2 using 1900a3b-E.tex, append adjr2 dec(3)  ctitle(Two Year Lag)
xtreg lib L5.ppol i.year  if regime>0 & PolLevelInd1==1, fe vce(cluster id)
outreg2 using 1900a3b-E.tex, append adjr2 dec(3)  ctitle(Five Year Lag)

xtreg lib c.ppol##i.PolLevelInd1 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a3c-E.tex, replace adjr2 dec(3)  ctitle(Analysis Year)
xtreg lib c.L1.ppol##i.L1.PolLevelInd1 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a3c-E.tex, append adjr2 dec(3)  ctitle(One Year Lag)
xtreg lib c.L2.ppol##i.L2.PolLevelInd1 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a3c-E.tex, append adjr2 dec(3)  ctitle(Two Year Lag)
xtreg lib c.L5.ppol##i.L5.PolLevelInd1 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a3c-E.tex, append adjr2 dec(3)  ctitle(Five Year Lag)

xtreg lib c.ppol##PolLevelInd1 i.year if regime>0, fe vce(cluster id)
margins PolLevelInd1, at(ppol=(0(0.2)1))
marginsplot, scheme(s1mono) ///
	title("") 	plot( , label("All Other Cases" "Periods of Elevated Polarization, and Above")) ///
	ytitle("Predicted Value of Liberal Democracy") xtitle("V-Dem Political Polarization Measure")
graph export "C:\Users\evanl\Desktop\Polarization\Final Analysis\Figure 3a-E.pdf", as(pdf) replace
marginsplot, scheme(s1mono) ///
	title("") ///
 	plot( , label("All Other Cases" "Periods of Elevated Polarization, and Above")) ///
	ytitle("Predicted Value of Liberal Democracy") xtitle("V-Dem Political Polarization Measure")
graph export "C:\Users\evanl\Desktop\Polarization\Final Analysis\Final Tables and Figures\Appendices\Appendix B Figure 5.tif", as(tif) replace

//Heightened (above .565625) - top quartile

xtreg lib ppol i.year if regime>0 & PolLevelInd2==0, fe vce(cluster id)
outreg2 using 1900a3a-H.tex, replace adjr2 dec(3)  ctitle(Analysis Year)
xtreg lib L1.ppol i.year if regime>0 & PolLevelInd2==0, fe vce(cluster id)
outreg2 using 1900a3a-H.tex, append adjr2 dec(3)  ctitle(One Year Lag)
xtreg lib L2.ppol i.year if regime>0 & PolLevelInd2==0, fe vce(cluster id)
outreg2 using 1900a3a-H.tex, append  adjr2 dec(3) ctitle(Two Year Lag)
xtreg lib L5.ppol i.year if regime>0 & PolLevelInd2==0, fe vce(cluster id)
outreg2 using 1900a3a-H.tex, append adjr2 dec(3)  ctitle(Five Year Lag)

xtreg lib ppol i.year  if regime>0 & PolLevelInd2==1, fe vce(cluster id)
outreg2 using 1900a3b-H.tex, replace adjr2 dec(3)  ctitle(Analysis Year)
xtreg lib L1.ppol i.year if regime>0 & PolLevelInd2==1, fe vce(cluster id)
outreg2 using 1900a3b-H.tex, append adjr2 dec(3)  ctitle(One Year Lag)
xtreg lib L2.ppol i.year if regime>0 & PolLevelInd2==1, fe vce(cluster id)
outreg2 using 1900a3b-H.tex, append adjr2 dec(3)  ctitle(Two Year Lag)
xtreg lib L5.ppol i.year  if regime>0 & PolLevelInd2==1, fe vce(cluster id)
outreg2 using 1900a3b-H.tex, append adjr2 dec(3)  ctitle(Five Year Lag)

xtreg lib c.ppol##i.PolLevelInd2 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a3c-H.tex, replace adjr2 dec(3)  ctitle(Analysis Year)
xtreg lib c.L1.ppol##i.L1.PolLevelInd2 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a3c-H.tex, append  adjr2 dec(3) ctitle(One Year Lag)
xtreg lib c.L2.ppol##i.L2.PolLevelInd2 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a3c-H.tex, append  adjr2 dec(3) ctitle(Two Year Lag)
xtreg lib c.L5.ppol##i.L5.PolLevelInd2 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a3c-H.tex, append adjr2 dec(3)  ctitle(Five Year Lag)

xtreg lib c.ppol##PolLevelInd2 i.year if regime>0, fe vce(cluster id)
margins PolLevelInd2, at(ppol=(0(0.2)1))
marginsplot, scheme(s1mono) ///
	title("") 	plot( , label("All Other Cases" "Periods of Heightened or Severe Polarization")) ///
	ytitle("Predicted Value of Liberal Democracy") xtitle("V-Dem Political Polarization Measure")
graph export "C:\Users\evanl\Desktop\Polarization\Final Analysis\Figure 3a-H.pdf", as(pdf) replace
marginsplot, scheme(s1mono) ///
	title("") 	///
	plot( , label("All Other Cases" "Periods of Heightened or Severe Polarization")) ///
	ytitle("Predicted Value of Liberal Democracy") xtitle("V-Dem Political Polarization Measure")
	graph export "C:\Users\evanl\Desktop\Polarization\Final Analysis\Final Tables and Figures\Appendices\Appendix B Figure 6.tif", as(tif) replace

//Severe (above 0.722) - top 5% (640 obs)

xtreg lib ppol i.year if regime>0 & PolLevelInd3==0, fe vce(cluster id)
outreg2 using 1900a3a-S.tex, replace adjr2 dec(3)  ctitle(Analysis Year)
xtreg lib L1.ppol i.year if regime>0 & PolLevelInd3==0, fe vce(cluster id)
outreg2 using 1900a3a-S.tex, append  adjr2 dec(3) ctitle(One Year Lag)
xtreg lib L2.ppol i.year if regime>0 & PolLevelInd3==0, fe vce(cluster id)
outreg2 using 1900a3a-S.tex, append  adjr2 dec(3) ctitle(Two Year Lag)
xtreg lib L5.ppol i.year if regime>0 & PolLevelInd3==0, fe vce(cluster id)
outreg2 using 1900a3a-S.tex, append  adjr2 dec(3) ctitle(Five Year Lag)

xtreg lib ppol i.year  if regime>0 & PolLevelInd3==1, fe vce(cluster id)
outreg2 using 1900a3b-S.tex, replace adjr2 dec(3)  ctitle(Analysis Year)
xtreg lib L1.ppol i.year if regime>0 & PolLevelInd3==1, fe vce(cluster id)
outreg2 using 1900a3b-S.tex, append adjr2 dec(3)  ctitle(One Year Lag)
xtreg lib L2.ppol i.year if regime>0 & PolLevelInd3==1, fe vce(cluster id)
outreg2 using 1900a3b-S.tex, append  adjr2 dec(3) ctitle(Two Year Lag)
xtreg lib L5.ppol i.year  if regime>0 & PolLevelInd3==1, fe vce(cluster id)
outreg2 using 1900a3b-S.tex, append  adjr2 dec(3) ctitle(Five Year Lag)

xtreg lib c.ppol##i.PolLevelInd3 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a3c-S.tex, replace adjr2 dec(3)  ctitle(Analysis Year)
xtreg lib c.L1.ppol##i.L1.PolLevelInd3 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a3c-S.tex, append  adjr2 dec(3) ctitle(One Year Lag)
xtreg lib c.L2.ppol##i.L2.PolLevelInd3 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a3c-S.tex, append  adjr2 dec(3) ctitle(Two Year Lag)
xtreg lib c.L5.ppol##i.L5.PolLevelInd3 i.year  if regime>0, fe vce(cluster id)
outreg2 using 1900a3c-S.tex, append adjr2 dec(3) ctitle(Five Year Lag)

xtreg lib c.ppol##PolLevelInd3 i.year if regime>0, fe vce(cluster id)
margins PolLevelInd3, at(ppol=(0(0.2)1))
marginsplot, scheme(s1mono)  ///
	title("") 	plot( , label("All Other Cases" "Periods of Severe Polarization")) ///
	ytitle("Predicted Value of Liberal Democracy") xtitle("V-Dem Political Polarization Measure")
graph export "C:\Users\evanl\Desktop\Polarization\Final Analysis\Figure 3a-S.pdf", as(pdf) replace
marginsplot, scheme(s1mono)  ///
	title("") 	///
 	plot( , label("All Other Cases" "Periods of Severe Polarization")) ///
	ytitle("Predicted Value of Liberal Democracy") xtitle("V-Dem Political Polarization Measure")
	graph export "C:\Users\evanl\Desktop\Polarization\Final Analysis\Final Tables and Figures\Appendices\Appendix B Figure 7.tif", as(tif) replace

